package com.iapplize.gcm.test;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gcm.GCMBaseIntentService;
import com.iapplize.gcm.test.R;
import com.iapplize.objects.GlobalVariables;

public class GCMIntentService extends GCMBaseIntentService {

	MyApplication appState;

	public GCMIntentService() {
		super(GlobalVariables.getSenderId());
		// SENDER_ID is my project id into google account url
		// TODO Auto-generated constructor stub
		Log.d(TAG, "[GCMIntentService] start");

	}

	public GCMIntentService(String senderId) {
		super(senderId);
		// TODO Auto-generated constructor stub
		Log.d(TAG, "[GCMIntentService] start - sender Id : " + senderId);

	}

	@Override
	protected void onMessage(Context ctx, Intent intent) {
		// TODO Auto-generated method stub

		Bundle extras = intent.getExtras();
		String message = "";

		if (extras != null) {

			message = extras.getString("message");
		}

		appState = ((MyApplication) ctx.getApplicationContext());
		if (appState.mActivity instanceof EnteringActivety) {

			appState.mActivity.runOnUiThread(new Runnable() {
				public void run() {

					if (((EnteringActivety) appState.mActivity).spinnerDialog != null) {
						((EnteringActivety) appState.mActivity).spinnerDialog
								.cancel();
					}

				}
			});
		} else if (appState.mActivity instanceof AllUsersActivity) {

			appState.mActivity.runOnUiThread(new Runnable() {
				public void run() {

					if (((AllUsersActivity) appState.mActivity).spinnerDialog != null) {
						((AllUsersActivity) appState.mActivity).spinnerDialog
								.cancel();
					}

				}
			});
		}

		String ns = Context.NOTIFICATION_SERVICE;
		NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

		int icon = R.drawable.ic_launcher;
		CharSequence tickerText = getString(R.string.app_name);
		long when = System.currentTimeMillis();

		Notification notification = new Notification(icon, tickerText, when);

		Context context = getApplicationContext();
		CharSequence contentTitle = getString(R.string.app_name);
		CharSequence contentText = message;
		notification.defaults |= Notification.DEFAULT_SOUND;
		notification.defaults |= Notification.DEFAULT_VIBRATE;

		PendingIntent contentIntent;

		Intent i = new Intent(context, PushAlertActivity.class);
		i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
		contentIntent = PendingIntent.getActivity(appState, 0, i, 0);

		notification.setLatestEventInfo(context, contentTitle, contentText,
				contentIntent);

		mNotificationManager.notify(1, notification);

		final Bundle bundle = intent.getExtras();

		Intent intent2 = new Intent(ctx, PushAlertActivity.class);
		intent2.putExtras(bundle);
		intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		ctx.startActivity(intent2);

	}

	@Override
	protected void onRegistered(Context ctx, String regId) {
		// TODO Auto-generated method stub
		// sendToServer(ctx,regId );
		appState = ((MyApplication) ctx.getApplicationContext());
		if (appState.mActivity != null) {

			if (appState.mActivity instanceof EnteringActivety) {

				appState.mActivity.runOnUiThread(new Runnable() {
					public void run() {

						((EnteringActivety) appState.mActivity).gcm_status
								.setText(R.string.GCM_Ready);

					}
				});

			}
		}
	}

	@Override
	protected void onUnregistered(Context ctx, String arg1) {
		// TODO Auto-generated method stub
		appState = ((MyApplication) ctx.getApplicationContext());
		if (appState.mActivity != null) {

			if (appState.mActivity instanceof EnteringActivety) {

				appState.mActivity.runOnUiThread(new Runnable() {
					public void run() {

						((EnteringActivety) appState.mActivity).gcm_status
								.setText(R.string.GCM_Faild);

					}
				});
			}

		}
	}

	/*
	 * private void sendToServer(Context ctx,String regId){ HttpConnect hc1 =
	 * new HttpConnect(ctx, HttpConnect.GCM_TEST_SERVER); hc1.execute(regId); }
	 */

	@Override
	protected void onError(Context ctx, String arg1) {
		// TODO Auto-generated method stub
		appState = ((MyApplication) ctx.getApplicationContext());
		if (appState.mActivity != null) {

			if (appState.mActivity instanceof EnteringActivety) {

				appState.mActivity.runOnUiThread(new Runnable() {
					public void run() {

						((EnteringActivety) appState.mActivity).gcm_status
								.setText(R.string.GCM_Faild);

					}
				});
			}

		}
	}

}
